var logging = {};
var popUp = null;
var root = 'C:/technisys/';
var path = 'C:/technisys/journal/';
(function(){
	var Level = function(name, value){
		this.nameLevel = name;
		this.valueLevel = value;
		
			
	};
	
	Level.prototype = {
			getName : function(){
				return this.nameLevel;
			},
			intValue : function(){
				return this.valueLevel;
			}
	};
	
	Level.ALL = new Level("ALL", 0);

	Level.TRACE = new Level("TRACE", 100);

	Level.DEBUG = new Level("DEBUG", 200);

	Level.INFO = new Level("INFO", 300);

	Level.WARN = new Level("WARN", 400);

	Level.ERROR = new Level("ERROR", 500);

	Level.FATAL = new Level("FATAL", 600);

	Level.OFF = new Level("OFF", 1000);
	
	logging.Level = Level;
	
	var LogRecord = function (level, msg){
		if(arguments.length !=2)
			throw 'Invalid length arguments, must be 2';		
		if(level == null || ! (level instanceof Level))
			throw "Invalid logging level value";			
		if(msg == null || !(typeof msg == 'string'))
			throw 'Invalid messege value';		
		
	    this.levelLogRecord = level;	    
	    this.sourceClassName = new String('');
	    this.sourceMethodName = new String('');
	    this.message = msg;
	    this.thrown = new Error('');
	    this.loggerName = null;
	    this.parameters = new Object();
	};
	
	LogRecord.prototype = {
			getLevel : function(){
		    	return this.levelLogRecord;
		    },
		    setLevel : function(param){
		    	this.levelLogRecord = param;
		    },
		    getSourceClassName : function(){
		    	return this.sourceClassName;
		    },
		    setSourceClassName : function(param){
		    	if(param == null || !(typeof param == 'string'))
		    		throw 'Invalid param type, must be String';		    	
		    	this.sourceClassName = param;
		    },
		    getSourceMethodName : function(){
		    	return this.sourceMethodName;
		    },
		    setSourceMethodName : function(param){
		    	if(param == null || !(typeof param == 'string'))
		    		throw 'Invalid param type, must be String';		    	
		    	this.sourceMethodName = param;
		    },
		    getMessage : function(){
		    	return this.message;
		    },
		    setMessage : function(param){
		    	if(param == null || !(typeof param == 'string'))
		    		throw 'Invalid param type, must be String';		    	
		    	this.message = param;
		    },
		    getThrown : function(){
		    	return this.thrown;
		    },
		    setThrown : function(param){
		    	if(param == null || !(typeof param == 'object'))
		    		throw 'Invalid param type, must be Error';		    	
		  		this.thrown = param;
		    },
		    getLoggerName : function(){
		    	return this.loggerName;
		    },
		    setLoggerName : function(param){
		    	if (param == null || ! (param =='string'))
		    		throw 'Invalid LogRecord';		    	    		
		    	this.loggerName = param;
		    },
		    getParameters : function(){
		    	return this.parameters;
		    },
		    setParameters : function(param){
		    	this.parameters = param;
		    }
	};    

	
	logging.LogRecord = LogRecord;
	
	var Formatter = function(){
		/*
		 * Empty constructor
		 * */		
	};
	
	Formatter.prototype = {
			format : function (record){
				/*
				 * Abstract function
				 * */		
			}, 
			getHead : function (handler){
				return '';
			},
			getTail : function (handler){
				return '';
			},
			/*
			 * Returns the day of the month (from 1-31)
			 * %F
			 * */
			getDate : function(){
				var d = (new Date()).getDate();
				return d < 10 ? '0' + d : d;
			},
			/*
			 * Returns the day of the week (from 0-6)
			 * %D
			 * */
			getDay : function(){
				var d = (new Date()).getDay();
				return '0' + d;
			},
			/*
			 * Returns the year (four digits)
			 * %Y
			 * */
			getFullYear : function(){
				var d = (new Date()).getFullYear();
				return d;	
			},
			/*
			 * Returns the hour (from 0-23)
			 * %H
			 * */
			getHours : function(){
				var d = (new Date()).getHours();
				return d < 10 ? '0' + d : d;	
			},
			/*
			 * Returns the milliseconds (from 0-999)
			 * %L
			 * */
			getMilliseconds : function(){
				var d = (new Date()).getMilliseconds();
				return d < 10 ? '00' + d : d < 100 ? '0' + d : d;	
			},
			/*
			 * Returns the month (from 0-11)
			 * %B
			 * */
			getMonth : function(){
				var d = (new Date()).getMonth();
				return d < 10 ? '0' + (d + 1) : d + 1;	
			},
			/*
			 * Returns the seconds (from 0-59)
			 * %S
			 * */
			getSeconds : function(){
				var d = (new Date()).getSeconds();
				return d < 10 ? '0' + d : d;	
			},
			/*
			 * Returns the seconds (from 0-59)
			 * %M
			 * */
			getMinutes : function(){
				var d = (new Date()).getMinutes();
				return d < 10 ? '0' + d : d;	
			}
	};
	
	Formatter.formatMessage = function (handler){
		if (handler == null || ! (handler instanceof LogRecord))
			throw 'Invalid Handler';
		
		var f = new Formatter();
		
		//Replace Date Time
		var msg = handler.getLevel().getName() + ' - ' + handler.getMessage();
		msg = msg.replace('%F', f.getDate());
		msg = msg.replace('%D',	f.getDay());
		msg = msg.replace('%Y', f.getFullYear());
		msg = msg.replace('%H',	f.getHours());
		msg = msg.replace('%L', f.getMilliseconds());
		msg = msg.replace('%M', f.getMinutes());
		msg = msg.replace('%S', f.getSeconds());
		msg = msg.replace('%B', f.getMonth());

		//Replace Extra Info
		msg = msg.replace('%c', handler.getSourceClassName());
		msg = msg.replace('%m', handler.getSourceMethodName());
		msg = msg.replace('%e', handler.getThrown());
		msg = msg.replace('%p', handler.getParameters());
		return msg;
	};
	
	logging.Formatter = Formatter;
	
	/**
	 * @constructor
	 */	 
	var SimpleFormatter = function () {
			
	};
	
	SimpleFormatter.prototype = {
			format : function (logRecord){
				if (logRecord == null || ! (logRecord instanceof LogRecord))
					throw 'Invalid param, must be LogRecord type';				
				return Formatter.formatMessage(logRecord);	
			}
	};
	
	logging.SimpleFormatter = SimpleFormatter;
	
	var Handler = function (){
		/*
		 * Default constructor
		 * */
	};
	
	Handler.prototype = {
			publish : function(logRecord){
				/*
				 * Abstract method
				 * */				
			},
			close : function(){
				/*
				 * Abstract method
				 * */
			},
			flush : function(){
				/*
				 * Abstract method
				 * */
			}
	};
	logging.Handler = Handler;
	/*
	 * Inherits Handler
	 * */
	var ConsoleHandler = function() {
		/*
		 * Error message
		 * */
		this.info = '<div class="ui-widget"><div class="ui-state-highlight ui-corner-all" style="margin-top: 20px; padding: 0 .7em;"><p><span class="ui-icon ui-icon-info" style="float: left;margin-right: .3em;"></span>%m</p></div></div>';
		/*
		 * Info message
		 * */
		this.error = '<div class="ui-widget"><div class="ui-state-error ui-corner-all" style="padding: 0 .7em;"><p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"><span>%m</p></div></div>';
	};
	
	ConsoleHandler.prototype = {
			publish : function(logRecord, level){
				if (popUp == null || popUp.document == null){
					popUp = window.open('console.html','','directories=no,fullscreen=no,location=no,menubar=no,resizable=no,scrollbars=yes,status=no,titlebar=no,toolbar=no,width=300,height=400');
				}
				
				try {
					switch (level) {
					case 100:
					case 200:
					case 300:
						popUp.document.getElementById('consoleMessage').innerHTML = this.info.replace('%m', logRecord) + popUp.document.getElementById('consoleMessage').innerHTML;
						break;
					case 400:
					case 500:
					case 600:
						popUp.document.getElementById('consoleMessage').innerHTML = this.error.replace('%m', logRecord) + popUp.document.getElementById('consoleMessage').innerHTML; 
						break;
					default:
						popUp.document.getElementById('consoleMessage').innerHTML = this.error.replace('%m', logRecord) + popUp.document.getElementById('consoleMessage').innerHTML; 
						break;
					}
					popUp.focus();					
				} catch (e) {
					/*
					 * Do nothing
					 * */
				}
			}			
	};
	
	logging.ConsoleHandler = ConsoleHandler;
	/*
	 * Inherits Handler
	 * */
	var AjaxHandler = function() {

	};
	
	AjaxHandler.prototype = {
			publish : function(logRecord, level){
				var xmlhttp = new Object();
				if (window.XMLHttpRequest)				  
					xmlhttp=new XMLHttpRequest();				  
				else				  
				  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
				
				xmlhttp.open("POST","Logging",true);
				xmlhttp.setRequestHeader("Content-Type", 
                "application/x-www-form-urlencoded");
				xmlhttp.send("level=" + level + "&message=" + logRecord);
			}			
	};
	
	logging.AjaxHandler = AjaxHandler;	
	/*
	 * Inherits Handler
	 * */
	var FileHandler = function() {
		/*
		 * Manage path
		 * */
		this.fileSystemObject = new ActiveXObject("Scripting.FileSystemObject");
	};
	
	FileHandler.prototype = {
			publish : function(logRecord){				
				this.buildPath();
				var msg = this.openTextFile(this.fileDay());
				msg.WriteLine(message);
				msg.close(logRecord);								
			},
			fileDay : function (){
				var date = new Formatter();
				return date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate() + '.log';
			},
			buildPath : function(){
				if(!this.folderExists(root)){
					this.createFolder(root);
				}
				if(!this.folderExists(path)){
					this.createFolder(path);
				}
				var date = new Formatter();
				var folderDay = date.getFullYear() + '-' + date.getMonth();
				if(!this.folderExists(folderDay)){
					this.createFolder(folderDay);
				}
				
			},
			folderExists : function(folder){
				return this.fileSystemObject.FolderExists(folder);
			},
			fileExists : function (file){
				return this.fileSystemObject.FileExists(file);	
			},
			openTextFile : function (filename){
				return this.fileSystemObject.OpenTextFile(filename, 8, true, -2);	
			},
			createFolder : function(path){
				return this.fileSystemObject.CreateFolder(path);
			}
	};
	
	logging.FileHandler = FileHandler;
	
	/*
	 * Inherits Handler
	 * */
	var FileHandler = function() {
		this.fileSystemObject = new ActiveXObject("Scripting.FileSystemObject");
	};
	
	FileHandler.prototype = {
			createFolder : function (folder){
				fileSystemObject.CreateFolder(folder);
			},
			isFolderExists : function(folder){
				return fileSystemObject.FolderExists(folder);
			},
			publish : function(logRecord, level){

			},
			isFileExists : function (file){
				return fileSystemObject.FolderExists(file);	
			}
	};
	
	logging.FileHandler = FileHandler;
	
	var Logger = function (){
		this.handlerLogger = new Array();
		this.levelLogger = Level.TRACE;
		this.format = new SimpleFormatter();

	};	
	
	Logger.prototype = {
			addHandler : function(handler){				
				this.handlerLogger.push(handler);
			},
			removeAllHandler : function(){
				this.handlerLogger = new Array();
			},
			getHandlers : function(){
				return this.handlerLogger;
			},
			isLoggable : function(level){
				if(newLevel == null || !(newLevel instanceof Level))
					throw 'Invalid parameter, must be Level';
				return this.levelLogger.intValue() <= level.intValue();
			},
			setLevel : function(newLevel){
				if(newLevel == null || !(newLevel instanceof Level))
					throw 'Invalid parameter, must be Level';
				this.levelLogger = newLevel;
			},
			getLevel : function(){
				return this.levelLogger;
			},
			getFormatter : function (){
				return this.format;
			},
			setFormatter : function (param){			
				this.format = param;
			},
			trace : function(msg, parameters, error, className, methodName){
				if(levelLogger.intValue() >= Level.TRACE.intValue())
					return;				
				
				var logRecord = new LogRecord(this.levelLogger, msg);
				logRecord.setSourceMethodName(arguments[4] || '');
				logRecord.setSourceClassName(arguments[3] || '');
				logRecord.setThrown(arguments[2] || new Error(''));
				logRecord.setParameters(arguments[1] || {});
				
				
				for(var i = 0; i < this.handlerLogger.length; i++)
					this.handlerLogger[i].publish(this.format.format(logRecord), this.levelLogger.intValue());				
			},
			debug : function(msg, parameters, error, className, methodName){
				if(this.levelLogger.intValue() >= Level.DEBUG.intValue())
					return;				
				
				var logRecord = new LogRecord(this.levelLogger, msg);
				logRecord.setSourceMethodName(arguments[4] || '');
				logRecord.setSourceClassName(arguments[3] || '');
				logRecord.setThrown(arguments[2] || new Error(''));
				logRecord.setParameters(arguments[1] || {});
				
				for(var i = 0; i < this.handlerLogger.length; i++)
					this.handlerLogger[i].publish(this.format.format(logRecord), this.levelLogger.intValue());				
			},
			info : function(msg, parameters, error, className, methodName){
				if(this.levelLogger.intValue() >= Level.INFO.intValue())
					return;				
				
				var logRecord = new LogRecord(this.levelLogger, msg);
				logRecord.setSourceMethodName(arguments[4] || '');
				logRecord.setSourceClassName(arguments[3] || '');
				logRecord.setThrown(arguments[2] || new Error(''));
				logRecord.setParameters(arguments[1] || {});		
				
				for(var i = 0; i < this.handlerLogger.length; i++)
					this.handlerLogger[i].publish(this.format.format(logRecord), this.levelLogger.intValue());
				
			},
			warn : function(msg, parameters, error, className, methodName){
				if(this.levelLogger.intValue() >= Level.WARN.intValue())
					return;				
				
				var logRecord = new LogRecord(this.levelLogger, msg);
				logRecord.setSourceMethodName(arguments[4] || '');
				logRecord.setSourceClassName(arguments[3] || '');
				logRecord.setThrown(arguments[2] || new Error(''));
				logRecord.setParameters(arguments[1] || {});
				
				for(var i = 0; i < this.handlerLogger.length; i++)
					this.handlerLogger[i].publish(this.format.format(logRecord), this.levelLogger.intValue());				
			},
			error : function(msg, parameters, error, className, methodName){
				if(this.levelLogger.intValue() >= Level.ERROR.intValue())
					return;			
				
				var logRecord = new LogRecord(this.levelLogger, msg);
				logRecord.setSourceMethodName(arguments[4] || '');
				logRecord.setSourceClassName(arguments[3] || '');
				logRecord.setThrown(arguments[2] || new Error(''));
				logRecord.setParameters(arguments[1] || {});
				
				for(var i = 0; i < this.handlerLogger.length; i++)
					this.handlerLogger[i].publish(this.format.format(logRecord), this.levelLogger.intValue());			
			},
			fatal : function(msg, parameters, error, className, methodName){
				if(this.levelLogger.intValue() >= Level.FATAL.intValue())
					return;				
				
				var logRecord = new LogRecord(this.levelLogger, msg);
				logRecord.setSourceMethodName(arguments[4] || '');
				logRecord.setSourceClassName(arguments[3] || '');
				logRecord.setThrown(arguments[2] || new Error(''));
				logRecord.setParameters(arguments[1] || {});
				
				for(var i = 0; i < this.handlerLogger.length; i++)
					this.handlerLogger[i].publish(this.format.format(logRecord), this.levelLogger.intValue());				
			}
	};

	logging.Logger = Logger;
})();

var runtime = {};
(function(){
	var Runtime = function(){
		
	};
	
	Runtime.prototype = {
			exec : function(command){
				var WshShell = new ActiveXObject("WScript.Shell");
				var oExec = WshShell.Exec("calc");

				while (oExec.Status == 0)
				{
				     WScript.Sleep(100);
				}

				WScript.Echo(oExec.Status);
			} 
	};
	
	runtime.Runtime = Runtime; 
})();